package com.shooter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.shooter.level.ScoreManager;

/**
 *
 * @author Unni Veanchery Mana
 */
public final class Explosion {

    private int x = 50;
    private int y = 50;
    private static final int FRAME_COLS = 4;
    private static final int FRAME_ROWS = 4;
    private Texture explosionTexture;
    private static final String explosionSprite = "exp2.png";
    private float stateTime;
    private Animation explosion;
    private TextureRegion currentFrame;
    private TextureRegion[] explosionFrames;
    Sound explosionSnd;
    private ScoreManager scoreManager;
    public boolean showExplosion;
    private boolean showPlayerExplosion;
    private SpriteExplode se;
    
    public Explosion(ScoreManager scoreManager) {
        this.scoreManager = scoreManager;
        initilize();
    }

    private void initilize() {
        explosionTexture = new Texture(Gdx.files.internal(explosionSprite));
        explosionFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        TextureRegion[][] tmp = TextureRegion.split(explosionTexture, explosionTexture.getWidth() / FRAME_COLS, explosionTexture.getHeight() / FRAME_ROWS);
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                explosionFrames[index++] = tmp[i][j];
            }
        }
        // explosion = new Animation(0.09f, explosionFrames); for android
        explosion = new Animation(0.1f, explosionFrames);
        stateTime = 0f;
        explosionSnd = Gdx.audio.newSound(Gdx.files.internal("Explosion3.wav"));
        initializePlayerExplode();
    }

    public void explode(SpriteBatch spriteBatch, int x, int y, Player player) {
        if (!showExplosion) {
            return;
        }
        stateTime += Gdx.graphics.getRawDeltaTime();
        if (explosion.isAnimationFinished(stateTime)){
            stateTime = 0f;
            player.setDelta(5);
            GameTimer.isExplosion = false;
            explosionSnd.stop();
            scoreManager.calculatePlayerHit();
            showExplosion = false;
            return;
        } else {
            if(scoreManager.getPlayerHit()>3) {
              showPlayerExplosion = true;
              se.setAnimated(true);
              se.updatePosition(player.getPosition().x, player.getPosition().y);
            }
            player.setDelta(0);
            GameTimer.isExplosion = true;
            currentFrame = explosion.getKeyFrame(stateTime, false);
            spriteBatch.draw(currentFrame, (x - 15), (y - 5));
            explosionSnd.play();
        }
                playerExplode(spriteBatch);
    }
    
    private void playerExplode(SpriteBatch batch){
        if(showPlayerExplosion){
            se.drawSpriteExplosion(batch);
        }
    }
    
    private void initializePlayerExplode(){
        se = new SpriteExplode("ship3.png");
    }
}